<?php

namespace addon\hotel\api\controller;

use app\api\controller\BaseApi;
use app\model\hotel\Hotel as HotelModel;

class Apply extends BaseApi
{

    //申请入驻
    public function apply()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        $hotel_model = new \app\model\hotel\HotelApply();

        $apply_data = [
            'member_id'=>$this->member_id,
            'hotel_type'=>input('hotel_type',1),//酒店类型
            'hotel_name'=>input('hotel_name',''),//酒店名称
            'hotel_level'=>input('hotel_level',1),//酒店等级
            'province_id'=>input('province_id',0),
            'city_id'=>input('city_id',0),
            'district_id'=>input('district_id',0),
            'full_address'=>input('full_address',''),//所在区域
            'address'=>input('address',''),//详细地址
            'longitude'=>input('longitude',''),
            'latitude'=>input('latitude',''),
            'start_time'=>input('start_time',''),//营业的开始时间
            'end_time'=>input('end_time',''),//营业的结束时间
            //'direct'=>input('direct',0),//是否自营1是直营,0非自营
            'telphone'=>input('telphone',''),//联系电话
            'name'=>input('name',''),//联系人
            'hotel_img'=>input('hotel_img',''),//列表图
            'images'=>input('images',''),//详情图
            'front_pic_id_card'=>input('front_pic_id_card',''),//身份证正面图片
            'reverse_pic_id_card'=>input('reverse_pic_id_card',''),//身份证反面图片
            'id_card_name'=>input('id_card_name',''),//证件姓名
            'id_card_number'=>input('id_card_number',''),//证件号码
            'id_card_type'=>input('id_card_type',''),//证件类型
            'id_card_start_time'=>input('id_card_start_time',''),//证件有效期开始时间
            'id_card_end_time'=>input('id_card_end_time',''),//证件有效期结束时间
            'account_holder'=>input('account_holder',''),//开户人
            'bank_number'=>input('bank_number',''),//银行卡号
            'bank_type'=>input('bank_type',''),//银行卡类型
            'bank_name'=>input('bank_name',''),//开户行名称
            'bank_img'=>input('bank_img',''),
            'business_license'=>input('business_license',''),//营业执照图
            'business_license_number'=>input('business_license_number',''),//营业执照编号
            'business_license_type'=>input('business_license_type',0),//营业执照有效期类型
            'business_license_start_time'=>input('business_license_start_time',''),//营业执照开始日期
            'business_license_end_time'=>input('business_license_end_time',''),//营业执照结束日期
            'apply_state'=>0,

            'contact_email' => isset($this->params[ 'contact_email' ]) ? $this->params[ 'contact_email' ] : '',
            'legal_addr' => isset($this->params[ 'legal_addr' ]) ? $this->params[ 'legal_addr' ] : '',
            'legal_cert_validity_type' => isset($this->params[ 'legal_cert_validity_type' ]) ? $this->params[ 'legal_cert_validity_type' ] : 1,
            'busi_type' => isset($this->params[ 'busi_type' ]) ? $this->params[ 'busi_type' ] : 0,
            'bank_province' => isset($this->params[ 'bank_province' ]) ? $this->params[ 'bank_province' ] : '',
            'bank_city' => isset($this->params[ 'bank_city' ]) ? $this->params[ 'bank_city' ] : '',
            'bank_district' => isset($this->params[ 'bank_district' ]) ? $this->params[ 'bank_district' ] : '',
            'bank_province_name' => isset($this->params[ 'bank_province_name' ]) ? $this->params[ 'bank_province_name' ] : '',
            'bank_city_name' => isset($this->params[ 'bank_city_name' ]) ? $this->params[ 'bank_city_name' ] : '',
            'bank_district_name' => isset($this->params[ 'bank_district_name' ]) ? $this->params[ 'bank_district_name' ] : '',
        ];

        $start_time = explode(':',$apply_data['start_time']);

        $apply_data['start_time'] = $start_time[0] * 3600 + $start_time[1] * 60;

        $end_time = explode(':',$apply_data['end_time']);

        $apply_data['end_time'] = $end_time[0] * 3600 + $end_time[1] * 60;

        //dump($apply_data);die;
        if (empty($apply_data['telphone'])) {
            return $this->response($this->error('', '请输入联系人手机号'));
        }
        if (!validatePhoneNumber($apply_data['telphone'])) {
            return $this->response($this->error('', '手机号有误'));
        }
        if (empty($apply_data['id_card_number'])) {
            return $this->response($this->error('', '请输入证件号'));
        }
        if (!validateIDCard($apply_data['id_card_number'])) {
            return $this->response($this->error('', '证件号无效'));
        }

        $res = $hotel_model->apply($apply_data,$this->member_id);

        return $this->response($res);
    }


    //申请信息
    public function applyinfo()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);
        $hotel_model = new \app\model\hotel\HotelApply();
        $result = $hotel_model->getApplyInfo([
            ['member_id','=',$this->member_id],
        ]);

        if ($result['data']){
            $val                                      = transformSeckillTime($result['data']);
            $result['data']['start_time_show'] = "{$val['start_hour']}:{$val['start_minute']}";
            $result['data']['end_time_show']   = "{$val['end_hour']}:{$val['end_minute']}";
        }

        return $this->response($result);

    }


    //酒店信息
    public function enterInfo()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        $hotel_model = new \app\model\hotel\Hotel();

        $condition = [
            ['member_id','=',$this->member_id]
        ];

        $res = $hotel_model->getHotelInfo($condition);
        if ($res['data']){
            $val                                      = transformSeckillTime($res['data']);
            $res['data']['start_time_show'] = "{$val['start_hour']}:{$val['start_minute']}";
            $res['data']['end_time_show']   = "{$val['end_hour']}:{$val['end_minute']}";
        }
        return $this->response($res);
    }

    //修改酒店信息
    public function editHotel()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        $hotel_model = new HotelModel();
        $hotel_id = input("hotel_id", 0);
        $condition = array (
            [ "hotel_id", "=", $hotel_id ]
        );

        $apply_data = array (
            'hotel_name' => input('hotel_name', ''),
            'hotel_img' => input('hotel_img', ''),
            'telphone' => input('telphone', ''),
            'direct' => input('direct', ''),
            //'is_frozen' => input('is_frozen', ''),
            'status' => input('status', ''),
            'province_id' => input('province_id', ''),
            'city_id' => input('city_id', ''),
            'district_id' => input('district_id', ''),
            'address' => input('address', ''),
            'full_address' => input('full_address', ''),
            'longitude' => input('longitude', ''),
            'latitude' => input('latitude', ''),

            'traffic' => input('traffic', ''),
            'hotel_level' => input('hotel_level', 0),
            'business_license' => input('business_license', ''),
            'start_time' => input('start_time', ''),
            'end_time' => input('end_time', ''),
            'commission_rate' => input('commission_rate', ''),
            'images' => input('images', ''),
            'video_url' => input('video_url', ''),
            'introduce' => input('introduce', ''),
            'is_recommend' => input('is_recommend', 0),
            'recommend_reason' => input('recommend_reason', ''),
            'sort' => input('sort', ''),
            'label_format' => input('label_format', ''),
            'equipment_format' => input('equipment_format', ''),

            'hotel_type' => input('hotel_type', 1),
            'business_license_number' => input('business_license_number', ''),
            'business_license_type' => input('business_license_type', ''),
            'business_license_start_time' => input('business_license_start_time', ''),
            'business_license_end_time' => input('business_license_end_time', ''),
            'front_pic_id_card' => input('front_pic_id_card', ''),
            'reverse_pic_id_card' => input('reverse_pic_id_card', ''),
            'id_card_name' => input('id_card_name', ''),
            'id_card_number' => input('id_card_number', ''),
            'id_card_type' => input('id_card_type', ''),
            'id_card_start_time' => input('id_card_start_time', ''),
            'id_card_end_time' => input('id_card_end_time', ''),
            'account_holder' => input('account_holder', ''),
            'bank_number' => input('bank_number', ''),
            'bank_type' => input('bank_type', ''),

            'bank_name' => input('bank_name', ''),
            'bank_img' => input('bank_img', ''),
            'contact_email' => input('contact_email', ''),
            'legal_addr' => input('legal_addr', ''),
            'legal_cert_validity_type' => input('legal_cert_validity_type', 0),
            'busi_type' => input('busi_type', 1),
            'bank_province' => input('bank_province', ''),
            'bank_city' => input('bank_city', ''),
            'bank_district' => input('bank_district', ''),
            'bank_province_name' => input('bank_province_name', ''),
            'bank_city_name' => input('bank_city_name', ''),
            'bank_district_name' => input('bank_district_name', ''),
        );

        $start_time = explode(':',$apply_data['start_time']);

        $apply_data['start_time'] = $start_time[0] * 3600 + $start_time[1] * 60;

        $end_time = explode(':',$apply_data['end_time']);

        $apply_data['end_time'] = $end_time[0] * 3600 + $end_time[1] * 60;

        $result = $hotel_model->editHotel($apply_data, $condition);
        return $this->response($result);
    }

    //修改酒店标签
    public function editLabel()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        $hotel_model = new HotelModel();
        $hotel_id = input("hotel_id", 0);
        $condition = array (
            [ "hotel_id", "=", $hotel_id ]
        );
        $apply_data = [
            'label_format'=>input('label_format','')
        ];

        $result = $hotel_model->editHotel($apply_data, $condition);
        return $this->response($result);

    }

}